using 高校员工餐饮信息管理系统.Models;

namespace 高校员工餐饮信息管理系统.Services
{
    /// <summary>
    /// 用户服务实现
    /// </summary>
    public class UserService : IUserService
    {
        private readonly ILogger<UserService> _logger;
        private readonly IEmailService _emailService;
        
        public UserService(ILogger<UserService> logger, IEmailService emailService)
        {
            _logger = logger;
            _emailService = emailService;
        }
        
        public async Task<User?> AuthenticateAsync(string workId, string password)
        {
            _logger.LogInformation($"认证用户: {workId}");
            
            // TODO: 实际项目中应从数据库查询
            // 这里提供模拟数据用于演示
            await Task.Delay(100); // 模拟异步操作
            
            if (workId == "teacher001" && VerifyPassword(password, "hashed_password"))
            {
                return new User
                {
                    Id = 1,
                    WorkId = workId,
                    UserName = "张老师",
                    Email = "teacher001@university.edu.cn",
                    Role = "教职工",
                    IsFirstLogin = false,
                    LastLoginAt = DateTime.Now
                };
            }
            
            return null;
        }
        
        public async Task<bool> ResetPasswordAsync(string workId, string email)
        {
            _logger.LogInformation($"重置密码: {workId}, 邮箱: {email}");
            
            // TODO: 实际项目中应从数据库查询用户
            await Task.Delay(100);
            
            // 生成重置链接
            var resetToken = Guid.NewGuid().ToString();
            var resetLink = $"https://meal-system.university.edu.cn/reset-password?token={resetToken}";
            
            // 发送重置密码邮件
            return await _emailService.SendPasswordResetEmailAsync(email, resetLink);
        }
        
        public async Task<bool> ChangePasswordAsync(string workId, string oldPassword, string newPassword)
        {
            _logger.LogInformation($"修改密码: {workId}");
            
            // TODO: 实际项目中应验证旧密码并更新数据库
            await Task.Delay(100);
            
            var user = await GetUserByWorkIdAsync(workId);
            if (user != null && VerifyPassword(oldPassword, user.PasswordHash))
            {
                // 更新密码
                user.PasswordHash = HashPassword(newPassword);
                user.IsFirstLogin = false;
                return true;
            }
            
            return false;
        }
        
        public async Task<User?> GetUserByWorkIdAsync(string workId)
        {
            _logger.LogInformation($"获取用户信息: {workId}");
            
            // TODO: 实际项目中应从数据库查询
            await Task.Delay(100);
            
            if (workId == "teacher001")
            {
                return new User
                {
                    Id = 1,
                    WorkId = workId,
                    UserName = "张老师",
                    Email = "teacher001@university.edu.cn",
                    Role = "教职工",
                    PasswordHash = "hashed_password"
                };
            }
            
            return null;
        }
        
        private bool VerifyPassword(string password, string passwordHash)
        {
            // TODO: 实际项目中应使用BCrypt或类似算法验证密码
            return HashPassword(password) == passwordHash;
        }
        
        private string HashPassword(string password)
        {
            // TODO: 实际项目中应使用BCrypt或类似算法哈希密码
            return $"hashed_{password}";
        }
    }
}

